<template>
  <qx-table :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft>
      <q-btn outline text-color="primary" @click="handleSync('headoffice', $t('Misc.SynchronousHeadquarters'))">
        {{ $t('Misc.SynchronousHeadquarters') }}
      </q-btn>
      <q-btn outline text-color="primary" @click="handleSync('rcs', $t('Misc.SynchronizeRCSData'))">
        {{ $t('Misc.SynchronizeRCSData') }}
      </q-btn>
      <q-btn outline text-color="primary" @click="handleSync('toRcs', $t('Misc.SynchronizeToRCS'))">
        {{ $t('Misc.SynchronizeToRCS') }}
      </q-btn>
    </template>
    <template v-slot:Image="{ row }">
      <div class="full-width row justify-center">
        <CarrierIMG :carrierType="row.containerBusinessType" :imageUrl="row.floorPlanPath"
                    classes="col-6 q-ma-sm cursor-pointer cursor-hover-image rounded-borders" prefix="Container"
                    @click="handleModelInfo(row)"></CarrierIMG>
        <div class="column full-width items-center text-dark text-weight-medium text-body1">
          <div class="q-pb-sm q-pt-xs">{{ `${row.containerNo}_${row.containerName}` }}</div>
          <div>{{ `${row.containerDepth || 0}x${row.containerWidth || 0}x${row.containerHeight || 0}mm` }}</div>
        </div>
      </div>
    </template>
  </qx-table>
  <q-modal v-model="syncVisible" :header="header" width="400" @beforeClose="beforeClose" @confirm="submitSync">
    <qx-form ref="FormRef" v-model="formInfo" :option="syncOption" class="q-pr-md"></qx-form>
  </q-modal>
  <q-modal v-model="modalVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="formInfo" cols="2" headerWidth="120"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { postAPI } from 'src/api/index.js'
import { getRemoteData, setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { yesOrNo } from 'src/enum/index.js'
import CarrierIMG from 'src/views/CarrierCore/CarrierIMG.vue'
import { containerBizTypes } from 'src/views/CarrierCore/enum.js'
import { computed, onMounted, ref } from 'vue'
import { createDialog } from 'src/utils/utils.js'

const header = ref('')
const formInfo = ref({})
const FormRef = ref(null)
const AGVBrands = ref([])
const searchForm = ref({})
const syncVisible = ref(false)
const modalVisible = ref(false)
const bindingModelVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/agvMasDataContainerType/list' })
getRemoteData('agv_brand', '/yfagv/api/agvCfgBrand/list').then(data => (AGVBrands.value = data))
const isCTU = computed(() => formInfo.value.containerBusinessType === '2')
const isForklift = computed(() => formInfo.value.containerBusinessType === '1')
const syncOption = computed(() => {
  return {
    span: 22, labelWidth: 90,
    column: [{
      label: `${i18n.t('CarrierCore.AGVBrand')}`, field: 'agvBrandCode', type: 'select', options: AGVBrands.value,
      props: { label: `${toI18nName('agvBrandNameZhCn')}`, value: 'agvBrandCode' }, required: true
    }]
  }
})
const tableOption = computed(() => {
  return {
    grid: true, searchSpan: 5, export: true, span: 23,
    column: [{
      label: `${i18n.t('CarrierCore.ContainerType')}`, field: 'containerName', minWidth: 100, hide: true,
      format: (_, row) => `${row.containerNo}_${row.containerName}`
    }, {
      label: `${i18n.t('CarrierCore.ContainerBizType')}`, field: 'containerBusinessType', type: 'select',
      options: containerBizTypes.value, minWidth: 100, useTag: true, topRight: true
    }, {
      label: '', field: 'Image', slot: true, export: false, minWidth: 100, search: false, nonHeader: true,
      display: false, showInfo: false
    }, {
      label: `${i18n.t('CarrierCore.ContainerSize')}`, field: 'containerSize', minWidth: 100, hide: true, search: false,
      format: (_, row) => `${row.containerDepth || 0}x${row.containerWidth || 0}x${row.containerHeight || 0}mm`
    }, {
      label: `${i18n.t('AGVCore.FullForkHeight')}(mm)`, field: 'forkFullHeight', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isForklift.value
    }, {
      label: `${i18n.t('AGVCore.EmptyForkHeight')}(mm)`, field: 'forkEmptyHeight', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isForklift.value
    }, {
      label: `${i18n.t('CarrierCore.RackLowOffset')}(mm)`, field: 'shelfLowDeviation', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('CarrierCore.RackHighOffset')}(mm)`, field: 'shelfHeightDeviation', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('AGVCore.VehicleLowOffset')}(mm)`, field: 'lowBodyDeviation', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('AGVCore.VehicleHighOffset')}(mm)`, field: 'bodyHeightDeviation', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('AGVCore.InwardExtension')}(mm)`, field: 'inCarExtension', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('CarrierCore.OutwardExtension')}(mm)`, field: 'outCargoStretch', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('AGVCore.QRCodeHeight')}(mm)`, field: 'qrCodeHeight', minWidth: 100,
      hide: true, search: false, headerClasses: 'table-header-inherit', showInfo: isCTU.value
    }, {
      label: `${i18n.t('Misc.IsBlindLift')}`, field: 'isBlindLift', type: 'select', options: yesOrNo.value,
      useTag: true, hide: true, showInfo: isForklift.value
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', search: false, minWidth: 80, hide: true, display: false
    }]
  }
})

const handleModelInfo = row => {
  formInfo.value = row
  modalVisible.value = true
}
const handleSync = (source, title) => {
  if (source === 'headoffice') {
    createDialog({ msg: `${i18n.t('Misc.SyncTip3')}` }, () => {
      postAPI('/yfagv/api/syncAgvCfg/syncAgvMasDataContainerType', {}, () => startSearch('clear'))
    })
    return
  }
  header.value = title
  formInfo.value = { source }
  const msg = source === 'rcs' ? `${i18n.t('Misc.SyncTip1')}` : `${i18n.t('Misc.SyncTip2')}`
  createDialog({ msg }, () => {
    syncVisible.value = true
  }, () => {})
}
const formatSearchForm = () => ({ ...searchForm.value })
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('rack_type')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  syncVisible.value = false
  bindingModelVisible.value = false
}
const submitSync = mDone => {
  FormRef.value.validate((form, done) => {
    const uri = form.source === 'rcs' ? '/yfagv/api/syncRcsToFactoryApi/syncContainerType'
      : '/yfagv/api/syncRcsApi/syncRcsAgvMasDataContainerType'
    postAPI(uri, form, () => {
      startSearch(form.source === 'rcs' ? 'clear' : '')
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}

onMounted(startSearch)
</script>